using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CentralOffice.Admin.BusinessActions;
using DepartmentManager = CentralOffice.Admin.BusinessActions.Department;
using DepartmentTable = CentralOffice.Admin.Data.Departments.DepartmentsDataTable;
using DepartmentRow = CentralOffice.Admin.Data.Departments.DepartmentsRow;
using EmployeeTable = CentralOffice.Admin.Data.EmployeeDataSet.EmployeesDataTable;
using EmployeeRow = CentralOffice.Admin.Data.EmployeeDataSet.EmployeesRow;

namespace CentralOffice.WebClient.HumanResources.Manage
{
    public partial class EmployeeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DepartmentTable depts = DepartmentManager.GetTopDepartment();

                EmployeeTable employees = Employee.GetAll();

                foreach (DepartmentRow row in depts.Rows)
                {
                    if (!row.IsActive || row.IsDeleted) continue;
                    TreeNode node = new TreeNode();
                    node.Text = "[" + row.Name + "]";
                    node.Value = row.Id.ToString();
                    node.SelectAction = TreeNodeSelectAction.Expand;
                    //node.ImageUrl = "../../images/menu/system.gif";
                    node.ImageUrl = "~/images/menu/system.gif";
                    //node.ToolTip = row.DepartmentNumber;
                    //node.PopulateOnDemand = true;
                    //node.SelectAction = TreeNodeSelectAction.Select;

                    tvEmp.Nodes.Add(node);
                    node.Expanded = true;

                    PopulateEmployee(node, employees);

                    AddDepartment(node, employees);
                }
            }
        }

        private void AddDepartment(TreeNode node, EmployeeTable employees)
        {
            Guid id = new Guid(node.Value);
            DepartmentTable depts = DepartmentManager.GetSubDepartment(id);

            foreach (DepartmentRow dept in depts)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = "[" + dept.Name + "]"; //dept.Name;
                newNode.Value = dept.Id.ToString();
                newNode.Target = "hrms";
                //newNode.ImageUrl = "../../images/node_dept.gif";
                newNode.ImageUrl = "~/images/node_dept.gif";
                newNode.NavigateUrl = "DepartmentEmployees.aspx?deptid=" + dept.Id.ToString() + "&parentid=" + node.Value;
                //node.ToolTip = newNode.DepartmentNumber;
                //newNode.SelectAction = TreeNodeSelectAction.Expand;

                node.ChildNodes.Add(newNode);
                newNode.Expanded = newNode.Depth < 2;

                PopulateEmployee(newNode, employees);

                AddDepartment(newNode, employees);
            }
        }

        private void PopulateEmployee(TreeNode node, EmployeeTable employees)
        {
            EmployeeTable emps = Employee.GetEmployeesByDepartmentId(employees, new Guid(node.Value));
            foreach (EmployeeRow row in emps)
            {
                if (!row.IsActive || row.IsDeleted) continue;
                TreeNode employeeNode = new TreeNode();
                employeeNode.Text = row.Name;
                employeeNode.Value = row.Id.ToString();
                employeeNode.Target = "hrms";
                employeeNode.NavigateUrl = "EmployeeUpdate.aspx?empid=" + employeeNode.Value + "&parentid=" + node.Value;//"webform1.aspx";//
                employeeNode.ImageUrl = row.Gender == "女" ? "~/images/1-1.gif" : "~/images/0-1.gif";

                node.ChildNodes.Add(employeeNode);
            }
        }
    }
}
